/*******************************************************************************
* @Filename      : bsp_board.h

* @Description   : bsp_board.h

* @Version       : 1.0.0

* @Date          : 2023.12.2
*******************************************************************************/

#ifndef __BSP_BOARD_H
#define __BSP_BOARD_H

#include "lcd_config.h"

#define MM_PACKAGE_QFN_4X4

#if LCD_WIDTH == 240
#define USE_PD6_AS_MIC_PIN
#endif

#ifdef __cplusplus
 extern  {
#endif

/* Includes ------------------------------------------------------------------*/
#define FLASH_START_ADDRESS  0x0800C000
#define WRITE_IMAGE_CPT816S_DATA_IN_FLASH

/* Exported types ------------------------------------------------------------*/

/* Exported define -----------------------------------------------------------*/
//SWD Pins
//============================================================================
#define SWD_SWCLK_PORT            GPIOA
#define SWD_SWCLK_PIN             GPIO_Pin_14

#define SWD_SWDIO_PORT            GPIOA
#define SWD_SWDIO_PIN             GPIO_Pin_13

//FLASH Pins
//============================================================================
#define FLASH_CS_PORT             GPIOA
#define FLASH_CS_PIN              GPIO_Pin_15
#define FLASH_CS_MODE             GPIO_Mode_Out_PP

#define SPIFLASH_MISO_PORT        GPIOB
#define SPIFLASH_MISO_PIN         GPIO_Pin_4
#define SPIFLASH_MISO_MODE        GPIO_Mode_IPU
#define SPIFLASH_MISO_PIN_SOURCE  GPIO_PinSource4
#define SPIFLASH_MISO_PIN_ALT     GPIO_AF_0

#define SPIFLASH_MOSI_PORT        GPIOB
#define SPIFLASH_MOSI_PIN         GPIO_Pin_5
#define SPIFLASH_MOSI_MODE        GPIO_Mode_AF_PP
#define SPIFLASH_MOSI_PIN_SOURCE  GPIO_PinSource5
#define SPIFLASH_MOSI_PIN_ALT     GPIO_AF_0

#define SPIFLASH_CLK_PORT         GPIOB
#define SPIFLASH_CLK_PIN          GPIO_Pin_3
#define SPIFLASH_CLK_MODE         GPIO_Mode_AF_PP
#define SPIFLASH_CLK_PIN_SOURCE   GPIO_PinSource3
#define SPIFLASH_CLK_PIN_ALT      GPIO_AF_0

//LCD Pins
//============================================================================
#define SPILCD_SDA_PORT           GPIOA
#define SPILCD_SDA_PIN            GPIO_Pin_11
#define SPILCD_SDA_MODE           GPIO_Mode_AF_PP
#define SPILCD_SDA_PIN_SOURCE     GPIO_PinSource11
#define SPILCD_SDA_PIN_ALT        GPIO_AF_4

#define SPILCD_SCL_PORT           GPIOA
#define SPILCD_SCL_PIN            GPIO_Pin_10
#define SPILCD_SCL_MODE           GPIO_Mode_AF_PP
#define SPILCD_SCL_PIN_SOURCE     GPIO_PinSource10
#define SPILCD_SCL_PIN_ALT        GPIO_AF_7

#if defined(MM_PACKAGE_QFN_4X4)
#define LCD_CS_PORT               GPIOB
#define LCD_CS_PIN                GPIO_Pin_2
#define LCD_CS_MODE               GPIO_Mode_Out_PP
#else
#define LCD_CS_PORT               GPIOA
#define LCD_CS_PIN                GPIO_Pin_0
#define LCD_CS_MODE               GPIO_Mode_Out_PP
#endif

#define LCD_DC_PORT               GPIOB
#define LCD_DC_PIN                GPIO_Pin_10
#define LCD_DC_MODE               GPIO_Mode_Out_PP

#define LCD_RES_PORT              GPIOB
#define LCD_RES_PIN               GPIO_Pin_11
#define LCD_RES_MODE              GPIO_Mode_Out_PP

#if defined(MM_PACKAGE_QFN_4X4)
#define LCD_EN_PORT               GPIOB
#define LCD_EN_PIN                GPIO_Pin_6
#define LCD_EN_MODE               GPIO_Mode_Out_PP
#else
#define LCD_EN_PORT               GPIOB
#define LCD_EN_PIN                GPIO_Pin_1
#define LCD_EN_MODE               GPIO_Mode_Out_PP
#endif

#if defined(MM_PACKAGE_QFN_4X4)
#define LCD_BLK_PORT              GPIOB
#define LCD_BLK_PIN               GPIO_Pin_7
#define LCD_BLK_MODE              GPIO_Mode_Out_PP
#else
#define LCD_BLK_PORT              GPIOB
#define LCD_BLK_PIN               GPIO_Pin_2
#define LCD_BLK_MODE              GPIO_Mode_Out_PP
#endif

#define   __TOUCH_CTP816S__
// simulate I2C 
#define TP_SDA_PORT           GPIOB  // GPIOA
#define TP_SDA_PIN            GPIO_Pin_7  // GPIO_Pin_4
#define TP_SDA_MODE             GPIO_Mode_Out_PP

#define TP_SCL_PORT           GPIOB  // GPIOA
#define TP_SCL_PIN            GPIO_Pin_6  // GPIO_Pin_5
#define TP_SCL_MODE              GPIO_Mode_Out_PP

// TP IRQ PIN
#define TP_IRQ_PORT              GPIOA
#define TP_IRQ_PIN               GPIO_Pin_0
#define TP_IRQ_MODE              GPIO_Mode_FLOATING // GPIO_Mode_IPD
#define TP_IRQ_PIN_EXTI_Line     EXTI_Line0

// TP RESET PIN
#define TP_RESET_PORT              GPIOB
#define TP_RESET_PIN               GPIO_Pin_14
#define TP_RESET_MODE              GPIO_Mode_Out_PP

//UC Pins
//============================================================================
//#define UC_GPIOA_PORT             GPIOA
//#define UC_GPIOA_PIN              (0)
//#define UC_GPIOA_MODE             GPIO_Mode_AIN

//#define UC_GPIOB_PORT             GPIOB
//#define UC_GPIOB_PIN              (0)
//#define UC_GPIOB_MODE             GPIO_Mode_AIN

//Filament PWM Pins
//============================================================================
#define HEATER_EN_PORT           GPIOB
#define HEATER_EN_PIN            GPIO_Pin_0
#define HEATER_EN_MODE           GPIO_Mode_AF_PP
#define HEATER_EN_PIN_SOURCE     GPIO_PinSource0
#define HEATER_EN_PIN_ALT        GPIO_AF_4
#define HEATER_EN_RCC_AHBENR     RCC_AHBENR_GPIOB

#define HEATER_EN_RCC_APB2ENR_TIM   RCC_APB2ENR_TIM1
#define HEATER_EN_TIM               TIM1
#define HEATER_EN_TIM_OCMode_PWM    TIM_OCMode_PWM1
//=============================above is PWM1==================================
#define HEATER_EN_PIN_IN()       \
    GPIO_StructInit(&GPIO_InitStruct);\
    GPIO_InitStruct.GPIO_Pin   = HEATER_EN_PIN;\
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;\
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_FLOATING;\
    GPIO_Init(HEATER_EN_PORT, &GPIO_InitStruct);\

#define HEATER_EN_PIN_L() \
    GPIO_StructInit(&GPIO_InitStruct);\
    GPIO_InitStruct.GPIO_Pin   = HEATER_EN_PIN;\
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;\
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;\
    GPIO_Init(HEATER_EN_PORT, &GPIO_InitStruct);\
    GPIO_WriteBit(HEATER_EN_PORT, HEATER_EN_PIN, Bit_RESET);\

#define HEATER_EN_PIN_H() \
    GPIO_StructInit(&GPIO_InitStruct);\
    GPIO_InitStruct.GPIO_Pin   = HEATER_EN_PIN;\
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;\
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;\
    GPIO_Init(HEATER_EN_PORT, &GPIO_InitStruct);\
    GPIO_WriteBit(HEATER_EN_PORT, HEATER_EN_PIN, Bit_SET);\
//=============================above is GPIO==================================

//RES_EN pins
#define RES_EN_PORT              GPIOA
#define RES_EN_PIN               GPIO_Pin_7

#define RES_EN_PIN_IN()       \
    GPIO_StructInit(&GPIO_InitStruct);\
    GPIO_InitStruct.GPIO_Pin   = RES_EN_PIN;\
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;\
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_FLOATING;\
    GPIO_Init(RES_EN_PORT, &GPIO_InitStruct);\

#define RES_EN_PIN_L() \
    GPIO_StructInit(&GPIO_InitStruct);\
    GPIO_InitStruct.GPIO_Pin   = RES_EN_PIN;\
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;\
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;\
    GPIO_Init(RES_EN_PORT, &GPIO_InitStruct);\
    GPIO_WriteBit(RES_EN_PORT, RES_EN_PIN, Bit_RESET);\

#define RES_EN_PIN_H() \
    GPIO_StructInit(&GPIO_InitStruct);\
    GPIO_InitStruct.GPIO_Pin   = RES_EN_PIN;\
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;\
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;\
    GPIO_Init(RES_EN_PORT, &GPIO_InitStruct);\
    GPIO_WriteBit(RES_EN_PORT, RES_EN_PIN, Bit_SET);\


//MIC IRQ Pins
//============================================================================
#if defined(USE_PD6_AS_MIC_PIN)
//LQFP48 no PD6
#define MIC_PORT                GPIOD
#define MIC_PIN                 GPIO_Pin_6
#define MIC_MODE                GPIO_Mode_IPD
#define MIC_PIN_EXTI_PortSource EXTI_PortSourceGPIOD
#define MIC_PIN_EXTI_PinSource  EXTI_PinSource6
#define MIC_PIN_EXTI_Line       EXTI_Line6
#define MIC_PIN_EXTI_Trigger    EXTI_Trigger_Rising_Falling
#define MIC_IRQn                EXTI4_15_IRQn
#define MIC_PIN_RCC_AHBENR      RCC_AHBENR_GPIOD
#else
#define MIC_PORT                GPIOA
#define MIC_PIN                 GPIO_Pin_0
#define MIC_MODE                GPIO_Mode_IPD
#define MIC_PIN_EXTI_PortSource EXTI_PortSourceGPIOA
#define MIC_PIN_EXTI_PinSource  EXTI_PinSource0
#define MIC_PIN_EXTI_Line       EXTI_Line0
#define MIC_PIN_EXTI_Trigger    EXTI_Trigger_Rising_Falling
#define MIC_IRQn                EXTI0_1_IRQn
#define MIC_PIN_RCC_AHBENR      RCC_AHBENR_GPIOA
#endif

//CHG_DET IRQ Pins
//============================================================================
#define CHG_DET_PORT                GPIOB
#define CHG_DET_PIN                 GPIO_Pin_1
#define CHG_DET_MODE                GPIO_Mode_IPD
#define CHG_DET_PIN_EXTI_PortSource EXTI_PortSourceGPIOB
#define CHG_DET_PIN_EXTI_PinSource  EXTI_PinSource1
#define CHG_DET_PIN_EXTI_Line       EXTI_Line1
#define CHG_DET_PIN_EXTI_Trigger    EXTI_Trigger_Rising_Falling
#define CHG_DET_IRQn                EXTI0_1_IRQn
#define CHG_DET_PIN_RCC_AHBENR      RCC_AHBENR_GPIOB

//system voltage
#define SYS_VOLTAGE             (3.0)

/* Exported constants --------------------------------------------------------*/

/* Exported variables --------------------------------------------------------*/

/* Exported functions --------------------------------------------------------*/

#ifdef __cplusplus
}
#endif
#endif

/* End of File ---------------------------------------------------------------*/
